#include<iostream>
#include <type_traits>
#include "httplib.h"
#include "add_contact.pb.h"
#include <random>
#include <iomanip>
using namespace std;
using namespace httplib;

void PrintReq(add_contact::AddContactRequest& req)
{
   cout << "新增联系人:" << endl;
   cout << "联系人姓名: "<< req.name() << endl;
   cout << "联系人年龄:" << req.age() << endl;
   for(int j = 0 ; j < req.phone_size();j++)
   {
      const add_contact::AddContactRequest_Phone& phone = req.phone(j);
      cout << "联系人电话" << j+1 << ": " << phone.number();
      cout << " (" << phone.PhontType_Name(phone.type()) << ")" << endl;  
   } 
}

 string uuid()
 {
                std::random_device rd;
                // size_t num = rd(); //生成一个随机数，效率较低
                std::mt19937_64 generator(rd());
                // cout << generator() << endl;
                std::uniform_int_distribution<int> dist(0, 255);
                stringstream ss;
                for(int i = 0 ; i < 8 ; i++)
                {
                    ss << std::setw(2) << std::setfill('0') << std::hex << dist(generator);
                    if(i == 3 || i == 5 || i == 7)
                    ss << "-"; 
                }
                static std::atomic<size_t> seq(1);
                size_t num = seq.fetch_add(1);//每次自增
                for(int i = 7 ; i>=0 ;i--)
                {
                    ss << std::setw(2) << std::setfill('0') << std::hex << (num>>(i*8)&0xff);//每次取出一个字节
                    if(i == 6) ss << "-";
                }
                return ss.str();
 }

int main()
{
    Server server;
    server.Post("/contact_add",[]( const Request& req, Response& resp){
          cout << "接收到Post请求..." << endl;
          //1.反序列化出request
          add_contact::AddContactRequest add_req;
          add_contact::AddContactResponse add_resp;
          string uid = uuid();
          int status = 200;
          bool success = true;
          string resp_str;
          string err_desc;
          if(!add_req.ParseFromString(req.body))
          {
             success = false;
             err_desc  = "反序列化失败";
             status = 500;
             goto END; 
          }
         //2.处理请求 -- 这里简单处理打印即可
         PrintReq(add_req);
         //3.处理成功
         status = 200;
         add_resp.set_success(success);
         add_resp.set_uid(uid);
         //4.序列化
         if(!add_resp.SerializeToString(&resp_str))
         {
               success = false;
               status = 500;
               err_desc  = "序列化失败";
               goto END;     
         } 
         END:
           add_resp.set_success(success);
           add_resp.set_err_desc(err_desc);
           add_resp.set_uid(uid);
           bool is_ok = add_resp.SerializeToString(&resp_str);
           cout << "is_ok:" << is_ok << endl;
           cout << "success: " << success << " , err:" << err_desc << endl;
           resp.body = resp_str;
           resp.status = status;
           resp.set_header("content-type","application/protobuf");
    });
  
    //
    server.listen("0.0.0.0", 8083);
    return 0;
}